//
//  SearchViewController.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/3/17.
//  Copyright © 2017年 蓝布鲁. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchResultViewController.h"
#import "HotWordView.h"
#import "SearchModel.h"

@interface SearchViewController ()<UISearchResultsUpdating,UISearchControllerDelegate>

// 视图
@property (nonatomic,retain)UIScrollView *scrollView;                           // 搜索框放置的滚动层
@property (nonatomic,retain)SearchResultViewController *resultController;       // 显示搜索结果的Controller
@property (nonatomic,retain)HotWordView *hotWorldView;                          // 搜索热词

// 数据
@property (nonatomic,retain)NSMutableArray *hotwordData;

@end

@implementation SearchViewController

#pragma mark - 初始化方法
-(instancetype)init{
    if (self==[super init]) {
        // 设置标题&TabBar图标
        self.title = @"搜索";
        self.tabBarItem.image = [UIImage imageNamed:@"search_tabBar"];
        self.hidesBottomBarWhenPushed = false;
    }
    return self;
}

#pragma mark - 系统回调函数
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 1.初始化searchBar
    [self setupSearchBar];
    
    // 2.初始化热词View
    [self setupHotWorld];
    
    
    // 3.获取热词网络数据
    [self updateHotWorld];
    

}

#pragma mark - 自定义方法
// 初始化searchBar
-(void)setupSearchBar {
    // 初始化搜索结果显示Controller
    self.resultController = [[SearchResultViewController alloc] init];
    
    // 初始化searchBar
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.resultController];
    
    // 设置searchBar属性
    self.searchController.searchBar.frame = CGRectMake(0, 0, SCREEN_WIDTH, 60);
    self.searchController.searchBar.placeholder = @"搜索老师/活动";
    self.searchController.searchResultsUpdater = self;
    self.searchController.delegate = self;
    
    // 初始化scrollView(一定要将searchBar放在UIScrollView中,要不然会出现无法显示的bug)
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.searchController.searchBar];
}

// 初始化热词View
-(void)setupHotWorld {
    // 初始化热词view,高度设置0,会自动改变高度
    self.hotWorldView = [[HotWordView alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, 0)];
    [self.scrollView addSubview:self.hotWorldView];
    
    // 热词的回调
    __weak SearchViewController *weakSelf  = self;
    [self.hotWorldView hotWorldTapped:^(NSString *title) {
        // 跳转搜索页面
        weakSelf.searchController.searchBar.text = title;
        weakSelf.searchController.active = 1;
    }];
}

#pragma mark - 网络请求
// 获取热词数据
-(void)updateHotWorld {
    self.hotwordData = [[NSMutableArray alloc] init];
    [[NetworkTool shareInstance] requireMethodType:POSTType
                                         URLString:@"SDSY/getHotWord"
                                        parameters:nil
                                           success:^(NSDictionary *respondDictionary) {
                                               for (NSDictionary *dictionary in respondDictionary[@"Hotword"]) {
                                                   [self.hotwordData addObject:dictionary[@"hotword"]];
                                               }

                                               // 刷新view
                                               self.hotWorldView.titleArray = self.hotwordData;
                                               [self.hotWorldView updateView];
                                           } failure:^(NSError *error) {
                
                [SVProgressHUD showfailed];
            }];
}

// 获取搜索结果
-(void)updateSearchText:(NSString *)searchText {
    // 网络获取搜索结果
    [[NetworkTool shareInstance] requireMethodType:POSTType
                                         URLString:@"SDSY/Search"
                                        parameters:@{@"key": searchText}
                                           success:^(NSDictionary *respondDictionary) {

                                               NSMutableArray *resultArray = [[NSMutableArray alloc] init];

                                               // 提取数据到模型
                                               for (NSDictionary *dictionary in respondDictionary[@"search"]) {
                                                    SearchModel *model = [[SearchModel alloc] initWithDictionary:dictionary];
                                                   [resultArray addObject:model];
                                               }

                                               // 刷新视图
                                               self.resultController.searchResult = resultArray;
                                               self.resultController.searchViewController = self;
                                               [self.resultController.tableView reloadData];
                                           } failure:^(NSError *error) {
                                                  
                                                [SVProgressHUD showfailed];
            }];
    

}

#pragma mark - UISearchResultsUpdating回调方法
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    NSString *searchText = searchController.searchBar.text;
    [self updateSearchText:searchText];
}

-(void)willDismissSearchController:(UISearchController *)searchController {
    
    // 因为searchController的bug,需要调整一次热词的frame
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 向上移动热词的frame
        CGRect frame = self.hotWorldView.frame;
        frame.origin.y = frame.origin.y - 20;
        self.hotWorldView.frame = frame;
    });
    
}

@end
